﻿Imports System.Data.OleDb

Public Class frmQuyDinh
    Public Sub LoadData0()
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from QuyDinh where Ma=1"
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "QuyDinh")
        dtgrQDHS.AutoGenerateColumns = False
        dtgrQDHS.DataSource = dset.Tables("QuyDinh")
    End Sub
    Public Sub LoadData1()
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from QuyDinh where Ma=2"
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "QuyDinh")
        dtgvQDLop.AutoGenerateColumns = False
        dtgvQDLop.DataSource = dset.Tables("QuyDinh")
    End Sub
    Public Sub LoadData2()
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from QuyDinh where Ma >2"
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "QuyDinh")
        dtgvQDMonHoc.AutoGenerateColumns = False
        dtgvQDMonHoc.DataSource = dset.Tables("QuyDinh")
    End Sub

    Private Sub frmQuyDinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tctQuyDinh.TabPages.Item(0).Text = "Học sinh"
        tctQuyDinh.TabPages.Item(1).Text = "Lớp"
        tctQuyDinh.TabPages.Item(2).Text = "Môn học"
        LoadData0()
        LoadData1()
        LoadData2()
    End Sub


    Private Sub SửaToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SửaToolStripMenuItem2.Click
        Try
            Dim strQuery As String = "Update QuyDinh set NoiDung=" + Convert.ToDouble(txtDoTuoi.Text).ToString() + " where Ma=1"
            Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
            If cmd.ExecuteNonQuery() >= 0 Then
                LoadData0()
            Else
                MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                Return
            End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Return
        End Try
    End Sub

    Private Sub SửaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SửaToolStripMenuItem1.Click
        Try
            Dim strQuery As String = "Update QuyDinh set NoiDung=" + Convert.ToDouble(txtSiSo.Text).ToString() + " where Ma=2"
            Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
            If cmd.ExecuteNonQuery() >= 0 Then
                LoadData1()
            Else
                MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                Return
            End If
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Return
        End Try
    End Sub

    Private Sub SửaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SửaToolStripMenuItem.Click
        Try
            Dim strQuery As String = ""
            Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
            conn.Open()
            If txtSoMon.Text <> "" Then
                strQuery = "Update QuyDinh set NoiDung=" + Convert.ToDouble(txtSoMon.Text).ToString() + " where Ma=3"
                Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
                If cmd.ExecuteNonQuery() >= 0 Then
                    LoadData2()
                Else
                    MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                    Return
                End If
                conn.Close()
            End If

            If True Then
                strQuery = "Update QuyDinh set NoiDung=" + Convert.ToDouble(txtDiemChuan.Text).ToString() + " where Ma=4"
                Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
                If cmd.ExecuteNonQuery() >= 0 Then
                    LoadData2()
                Else
                    MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                    Return
                End If
            End If
            
            conn.Close()
        Catch ex As Exception
            MessageBox.Show("Có lỗi.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Return
        End Try
    End Sub

    Private Sub dtgrQDHS_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgrQDHS.MouseClick
        Try
            txtDoTuoi.Text = dtgrQDHS.CurrentRow.Cells("ColNoiDung").Value.ToString()
        Catch ex As Exception
            Return
        End Try

    End Sub

    Private Sub dtgvQDMonHoc_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgvQDMonHoc.MouseClick
        Try
            If dtgvQDMonHoc.CurrentRow.Cells("ColMaQDMonHoc").Value.ToString() = "3" Then
                txtSoMon.Text = dtgvQDMonHoc.CurrentRow.Cells("ColNoiDungMonHoc").Value
            Else
                txtSoMon.Text = ""
            End If
            If dtgvQDMonHoc.CurrentRow.Cells("ColMaQDMonHoc").Value.ToString() = "4" Then
                txtDiemChuan.Text = dtgvQDMonHoc.CurrentRow.Cells("ColNoiDungMonHoc").Value
            Else
                txtDiemChuan.Text = ""
            End If
        Catch ex As Exception
            Return
        End Try
    End Sub

    Private Sub dtgvQDLop_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgvQDLop.MouseClick
        Try
            txtSiSo.Text = dtgvQDLop.CurrentRow.Cells("ColNoiDungLop").Value.ToString()
        Catch ex As Exception
            Return
        End Try
    End Sub

    Private Sub ThêmMônHọcToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThêmMônHọcToolStripMenuItem.Click
        Dim frm As frmMonHoc = New frmMonHoc
        frm.Show()
    End Sub
End Class